package com.gift.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.gift.common.JsonMapper;
import com.gift.common.PaGing;
import com.gift.common.ServerResponse;
import com.gift.common.TimeHandler;
import com.gift.dao.AccByDeaMapper;
import com.gift.dao.GameAccountMapper;
import com.gift.pojo.AccByDea;
import com.gift.pojo.GameAccount;
import com.gift.pojo.UserTable;
import com.gift.service.DataQueryService;
import com.gift.vo.AccByDeaVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/22.
 */
@Controller
@RequestMapping("goHome")
public class GoHomeController {

    @Resource
    DataQueryService dataQueryService;
    @Resource
    AccByDeaMapper accByDeaMapper;
    @Resource
    GameAccountMapper gameAccountMapper;

    //跳转到下班的信息页面
    @RequestMapping("/home")
    public String goHome(){
        return "GoHomeController/goHome2";
    }



    //得到游戏账号流水（分页）
    @RequestMapping("/getGHAll")
    @ResponseBody
    public PaGing getAllGameAccount(int page, int rows,@RequestParam Map<String,String> map){
        Map<String,Object> mapO ;
        mapO = TimeHandler.findParmHandler(map);
        int shift= TimeHandler.getShift();
        mapO.put("shift",shift);
        return dataQueryService.getAllGameAccount(page,rows,mapO);
    }

    @RequestMapping("/saveGoHome")
    @ResponseBody
    public ServerResponse saveGoHome(String entities){
        String[] strings= StringUtils.split(entities,"\\|");
        ArrayList<String> allJsonList = new ArrayList<>();
        for(int i = 0;i<strings.length;i++){
            allJsonList.add(strings[i]);
        }
        ArrayList<AccByDea> allList = new ArrayList<>();
        for(String s:allJsonList){
            AccByDeaVO oneABDVo = JsonMapper.string2Obj(s, new TypeReference<AccByDeaVO>() {
            });
            AccByDea oneABD = new AccByDea();
            BeanUtils.copyProperties(oneABDVo,oneABD);
            BigDecimal worry=oneABD.getFinalscore().subtract(oneABD.getInitialscore());
            oneABD.setWorryscore(worry);
            Subject currentUser = SecurityUtils.getSubject();
            UserTable user=((UserTable) currentUser.getPrincipal());
            oneABD.setHandlers(user.getRelname());
            oneABD.setHandlerid(user.getUid());
            allList.add(oneABD);
        }
        int res = 0;
        for(AccByDea oneABD:allList){
            accByDeaMapper.updateByPrimaryKeySelective(oneABD);
            int GAccId = oneABD.getAid();
            GameAccount gameAccount=gameAccountMapper.selectByPrimaryKey(GAccId);
            if(oneABD.getFinalscore() != null){
                gameAccount.setInitialscore(oneABD.getFinalscore());
                gameAccountMapper.updateByPrimaryKeySelective(gameAccount);
            }
            res++;
        }
        if(res < 1){
            return ServerResponse.createByErrorMessage("下班信息提交失败");
        }
        return ServerResponse.createBySuccessMessage("下班信息提交成功");
    }



}
